<?php
System::import('php/lang/Comparable.interface.php');
System::import('php/lang/Object.class.php');

class Boolean extends Object implements Comparable {
	/**
	 * The boolean value of the receiver.
	 */
	private final $value;

	/**
	 * Constructs a new {@code Boolean} with its boolean value specified by
	 * {@code string}. If {@code string} is not {@code null} and is equal to
	 * "true" using a non-case sensitive comparison, the result will be a
	 * Boolean representing the primitive value {@code true}, otherwise it will
	 * be a Boolean representing the primitive value {@code false}.
	 * 
	 * @param String | Boolean
	 * the string representing a boolean value.
	 */
	public function __construct($value) {
		if (self::type($value) == self::$STRING || self::type($value) == self::$BOOL)
			$this->value = self::parseBoolean($value);
		else
			$this->value = FALSE;
	}
	
	/**
	 * Gets the primitive value of this boolean, either {@code true} or
	 * {@code false}.
	 * 
	 * @return this object's primitive value, {@code true} or {@code false}.
	 */
	public function booleanValue() {
		return $this->value;
	}
	
	/**
	 * Compares this instance with the specified object and indicates if they
	 * are equal. In order to be equal, {@code o} must be an instance of
	 * {@code Boolean} and have the same boolean value as this object.
	 * 
	 * @param o
	 * the object to compare this boolean with.
	 * @return {@code true} if the specified object is equal to this
	 * {@code Boolean}; {@code false} otherwise.
	 */
	
	public function equals(Object $o) {
		return ($o == $this) || (($o instanceof Boolean) && (value == self::valueOf($o->value)));
	}
	
	/**
	 * Compares this object to the specified boolean object to determine their
	 * relative order.
	 * 
	 * @param that
	 * the boolean object to compare this object to.
	 * @return 0 if the value of this boolean and the value of {@code that} are
	 * equal; a positive value if the value of this boolean is
	 * {@code true} and the value of {@code that} is {@code false}; a
	 * negative value if the value if this boolean is {@code false} and
	 * the value of {@code that} is {@code true}.
	 * @see java.lang.Comparable
	 * @since 1.5
	 */
	public function compareTo(Boolean $value) {
		if ($value == null) {
			throw new Exception('variavel esta nula.');
		}
		
		if ($this->value == $value->value) {
			return 0;
		}
		
		return $this->value ? 1 : - 1;
	}
	
	/**
	 * Returns an integer hash code for this boolean.
	 * 
	 * @return this boolean's hash code, which is {@code 1231} for {@code true}
	 * values and {@code 1237} for {@code false} values.
	 */
	public function hashCode() {
		return spl_object_hash(parent::hashCode() + ($this->value ? 1231 : 1237));
	}
	
	/**
	 * Returns a string containing a concise, human-readable description of this
	 * boolean.
	 * 
	 * @return "true" if the value of this boolean is {@code true}, "false"
	 * otherwise.
	 */
	public function __toString() {
		return String::valueOf($this->value);
	}
	
	/**
	 * Returns the {@code boolean} value of the system property identified by
	 * {@code string}.
	 * 
	 * @param string
	 * the name of the requested system property.
	 * @return {@code true} if the system property named by {@code string}
	 * exists and it is equal to "true" using case insensitive
	 * comparison, {@code false} otherwise.
	 * @see System#getProperty(String)
	 */
	public static function getBoolean(String $string) {
		if(is_null($string) || $string->length() == 0) {
			return false;
		}
		return ($this->parseBoolean($string->__toString()));
	}
	
	/**
	 * Parses the specified string as a {@code boolean}.
	 * 
	 * @param s
	 * the string representation of a boolean value.
	 * @return {@code true} if {@code s} is not {@code null} and is equal to
	 * {@code "true"} using case insensitive comparison, {@code false}
	 * otherwise.
	 * @since 1.5
	 */
	public static function parseBoolean(String $s) {
		$aux = new String('TRUE');
		
		return $aux->equalsIgnoreCase($s);
	}
	
	/**
	 * Parses the specified var as a boolean value.
	 * 
	 * @param string
	 * the string representation of a boolean value.
	 * @return {@code TRUE} if {@code string} is equal to "true" using
	 * case insensitive comparison, {@code FALSE} otherwise.
	 * @see #parseBoolean(String)
	 */
	public static function valueOf($string) {
		return $this->parseBoolean(new String($string)) ? TRUE : FALSE;
	}
}
?>